package viper.badRelays;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.Border;

import viper.main.StateManager;

/**
 * @author Kevin Sin
 * 
 * Revision number: 1
 * Date created: 1/17/10
 * Date of Last Back-up on SVN: none
 * 
 * <p>This class is responsible for the GUI.  It interacts w/ StateManger only
 * to display the right info to the user and responds to user actions.  A CardLayout
 * is used to develop different panels and integrate them easily.
 */
public class ViperBadRelaysGUI extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//Singleton structure.
	public final static ViperBadRelaysGUI INSTANCE = new ViperBadRelaysGUI();

	public static final String LOGGED_OUT_CARD = "LOGGED_OUT_CARD";
	public static final String RELAYS_CARD = "RELAYS_CARD";
	
	private JPanel cardPanel;
	private CardLayout cl;
	
	public RelaysPanel relaysPanel;
	private BadRelaysLoggedOutPanel badRelaysLoggedOutPanel;
	
	private JPanel framePanel;
	
	public JPanel getFramePanel() {
		return framePanel;
	}

	private ViperBadRelaysGUI() {

		setTitle("VIPER - Relay Program");
		
		setLayout(new BorderLayout());

		cardPanel = new JPanel();

		cl = new CardLayout();
		cardPanel.setLayout(cl);
		//cardPanel.setBackground(new Color(255, 255, 255));
		Border padding = BorderFactory.createEmptyBorder(10,10,10,10);
		cardPanel.setBorder(padding);

		relaysPanel = new RelaysPanel();
		badRelaysLoggedOutPanel = new BadRelaysLoggedOutPanel();
		badRelaysLoggedOutPanel.setBackground(Color.WHITE);
		
		//framePanel = new JPanel();
		//internalFrame.add(framePanel);
		
		
		cardPanel.add(relaysPanel, RELAYS_CARD);
		cardPanel.add(badRelaysLoggedOutPanel, LOGGED_OUT_CARD);

		getContentPane().add(cardPanel, BorderLayout.CENTER);
		
		//getContentPane().add(internalFrame, BorderLayout.PAGE_END);
	}

	/**
	 * Sets the size and displays the window.
	 */
	public void initialize(){
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);
		changeCard(LOGGED_OUT_CARD);
		setVisible(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				StateManager.INSTANCE.exit();
			}			
		});	 
		
		setIconImage(new ImageIcon("images/icon.gif").getImage());
	}
	
	/**
	 * @param cardName - which JPanel to display
	 * Changes the window title and updates each panel before showing that panel.
	 */
	public void changeCard(String cardName){
		if(cardName.equals(LOGGED_OUT_CARD))
		{
			setSize(500, 500);
			setTitle("VIPER - Relay Program - Logged Out");
		}
		else if(cardName.equals(RELAYS_CARD))
		{
			setSize(700, 600);
			setTitle("VIPER - Relay Program - Logged in as " + BadRelaysStateManager.INSTANCE.getUsername());
			//setUpInternalFrame();
		}	
		cl.show(cardPanel, cardName);
	}

	
}
